package com.cloud.mkz.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.mkz.core.common.constant.CacheConstants;
import com.cloud.mkz.core.common.util.ResponseResult;
import com.cloud.mkz.core.log.annotation.SysLog;
import com.cloud.mkz.admin.api.entity.RbacUser;
import com.cloud.mkz.admin.service.RbacUserService;
import com.cloud.mkz.core.mvc.controller.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 系统用户
 *
 * @author mkz code generator
 * @date 2021-03-19 16:15:25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/rbac/user" )
@Api(value = "rbacuser", tags = "系统用户管理")
public class RbacUserController extends BaseController {

    private final  RbacUserService rbacUserService;

    /**
     * 在线用户查询
     * @return Map<String, Object>
     */
    @ApiOperation(value = "在线用户查询", notes = "在线用户查询")
    @PostMapping("/online" )
    public ResponseResult getOnlineUserPage(Page page) {
        List<Object> objects = redisUtil.lGetAll(CacheConstants.SYSTEM_ONLINE_DETAILS);
        if(null==objects||objects.size()<1){
            objects = new ArrayList<>();
            return ResponseResult.ok(objects);
        }
        page.setRecords(objects).setTotal(objects.size());
        return ResponseResult.ok(page);
    }

    /**
     * 分页查询
     * @param page 分页对象
     * @param rbacUser 系统用户
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('admin_rbacuser_get')" )
    public ResponseResult getRbacUserPage(Page page, RbacUser rbacUser) {
        return ResponseResult.ok(rbacUserService.page(page, Wrappers.query(rbacUser)));
    }


    /**
     * 通过id查询系统用户
     * @param pkId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{pkId}" )
    @PreAuthorize("@pms.hasPermission('admin_rbacuser_get')" )
    public ResponseResult getById(@PathVariable("pkId" ) Long pkId) {
        return ResponseResult.ok(rbacUserService.getById(pkId));
    }

    /**
     * 新增系统用户
     * @param rbacUser 系统用户
     * @return R
     */
    @ApiOperation(value = "新增系统用户", notes = "新增系统用户")
    @SysLog("新增系统用户" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_rbacuser_add')" )
    public ResponseResult save(@RequestBody RbacUser rbacUser) {
        return ResponseResult.ok(rbacUserService.save(rbacUser));
    }

    /**
     * 修改系统用户
     * @param rbacUser 系统用户
     * @return R
     */
    @ApiOperation(value = "修改系统用户", notes = "修改系统用户")
    @SysLog("修改系统用户" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_rbacuser_edit')" )
    public ResponseResult updateById(@RequestBody RbacUser rbacUser) {
        return ResponseResult.ok(rbacUserService.updateById(rbacUser));
    }

    /**
     * 通过id删除系统用户
     * @param pkId id
     * @return R
     */
    @ApiOperation(value = "通过id删除系统用户", notes = "通过id删除系统用户")
    @SysLog("通过id删除系统用户" )
    @DeleteMapping("/{pkId}" )
    @PreAuthorize("@pms.hasPermission('admin_rbacuser_del')" )
    public ResponseResult removeById(@PathVariable Long pkId) {
        return ResponseResult.ok(rbacUserService.removeById(pkId));
    }

}
